// chat_list_html_ele :[],

/*群聊通知，现在群聊改成数组*/
P2P_CHAT_NTY: function (ws, event, commandName, bodyStr, p2PChatNtf) {
    var start = new Date().getTime();
    
    var items = p2PChatNtf.items;

    for (var i = 0; i < items.length; i++) {
        try {
            var item = items[i];

            var touid = item.touid;
            var uid = item.uid;
            if (uid == p2PChatNtf.other.i) {
                item.f = p2PChatNtf.other;
            } else {
                item.f = p2PChatNtf.self;
            }

            SiteImBsHandler.P2P_CHAT_NTY_item(ws, event, commandName, item, items.length);

            siteim.p2p_scroll_chat();
        } catch (e) {
            // error(e);
            throw e;
        }
    }
    var end = new Date().getTime();
    //  log("耗时：" + (end - start) + "ms");
},

P2P_CHAT_NTY_item: function (ws, event, commandName, bodyObj, msgCount) {

    /**
     * {
            "c": "大家好",
            "f": {
                "createtime": "2017-10-21 15:55:26",
                "id": 28,
                "ip": "192.168.1.31",
                "loginname": "18698557302",
                "nick": "谭",
                "pwd": "f8a3d267379624b7817b933d9b6c12e0",
                "roles": [
                    3
                ],
                "status": 1,
                "updatetime": "2017-10-21 15:55:22"
            },
            "g": 28,
            "t": 1508913156443
        }
     */
    var time = formatDateByTime(bodyObj.t, "hh:mm:ss");
    if (formatDateByTime(bodyObj.t, "MM-dd") != formatDateByTime(Date.parse(new Date()), "MM-dd")) {
        time = formatDateByTime(bodyObj.t, "MM-dd hh:mm:ss");
    }
    var fromnick = "游客";
    var from = bodyObj.f;
    var fromLv = 0;
    var fromavatar = "/img/avatar/beimi.jpeg";//默认头像
    var fromuid = 0;
    var fromMana = 0;
    if (from) {
        fromnick = from.n;
        fromuid = from.i;
        fromLv = from.l;
        fromMana = from.m;
        fromavatar = res_url(from.a);
    }

    var curruid = null;
    if (isLogined) {
        curruid = curruser.id;
    }

    var fromSelf = curruid == from.i;
    var ct = bodyObj.ct;


    /**
     * // tio-link-card
     * <li class="layim-chat-mine">
            <div class="layim-chat-user">
                <img src="//res.layui.com/images/fly/avatar/00.jpg">
                <cite>
                    <i>2018-10-2623:12:43</i>
                    纸飞机
                </cite>
            </div>
            <div class="layim-chat-text">
                d
            </div>
        </li>

        <li>
            <div class="layim-chat-user">
                <img src="//res.layui.com/images/fly/avatar/00.jpg">
                <cite>
                    Z_子晴
                    <i>2018-10-26 23:12:44</i>
                </cite>
            </div>
            <div class="layim-chat-text">
                d
            </div>
        </li>

     */
    //siteim_p2p_chat_ul

    var li = document.createElement("li");
    siteim_p2p_chat_ul.appendChild(li);
    if (fromSelf) {
        $(li).addClass("layim-chat-mine");//.className = "layim-chat-mine";
    }

    


    if (ct == 2) {  //CARD_LINK 超连接卡片
        $(li).addClass("tio-link-card");
    }
    if (ct == 5) {  //视频卡片
        $(li).addClass("tio-video-card");
    }
    if (ct == 6) {  //图片卡片
        $(li).addClass("tio-img-card");
    }
    $(li).attr("id", bodyObj.mid);


    

    var div = document.createElement("div");
    li.appendChild(div);
    div.className = "layim-chat-user";



    var img = document.createElement("img");
    div.appendChild(img);
    img.src = fromavatar;
    
    if (!fromSelf) {
        $(img).css("cursor", "pointer");
    }
    



    // if (containRole(from.r, roles.allow_read_doc)) {
    //     var $vip;
    //     if (fromSelf) {
    //         $vip = $('<i class="layui-badge fly-badge-vip vip_to_avatar_for_chat_mine" title="t-io文档付费标识">V</i>');
    //     } else {
    //         $vip = $('<i class="layui-badge fly-badge-vip vip_to_avatar_for_chat" title="t-io文档付费标识">V</i>');
    //     }
    //     $(li).append($vip);
    // }



    var $vip = createVipFlag(from.r);
	if ($vip) {
		if (fromSelf) {
            $vip.addClass("vip_to_avatar_for_chat_mine");
        } else {
            $vip.addClass("vip_to_avatar_for_chat");
        }
        
        $(li).append($vip);
	}















    var cite = document.createElement("cite");
    div.appendChild(cite);

    var nickEle = document.createTextNode(fromnick);
    var timeEle = document.createElement("i");
    var canUnSend = false;
    if (fromSelf) {
        cite.appendChild(timeEle);
        cite.appendChild(nickEle);
        canUnSend = true;
    } else {
        cite.appendChild(nickEle);
        cite.appendChild(timeEle);
        if (isSuper) {
            canUnSend = true;
        }
    }

    

    timeEle.innerHTML = time;


    var div2 = document.createElement("div");
    li.appendChild(div2);
    $(div2).attr("id", "c_" + bodyObj.mid);
    div2.className = "layim-chat-text";
    // div2.innerHTML = bodyObj.c;
    if (canUnSend) {
        context.attach('#c_' + bodyObj.mid, [
            // {header: 'Options'},
            // {text: '撤回', href: '#'},
            // {text: 'Open in new Window', href: '#'},
            // {divider: true},
            // {text: 'Copy', href: '#'},
            // {text: 'Dafuq!?', href: '#'}
            
            {
                text: '撤回', action: new siteim_chat_contextAction(bodyObj.mid, siteim.groupid).unSend
            }
        ]);
    }


    SiteImBsHandler.P2P_CHAT_NTY_content(ws, event, commandName, bodyObj, msgCount, div2);
    $(div2).emojiParse(siteim_emoji_option_parse);

    siteim_group_chat_displayed_count++;

    siteim_notread_count++;
    ele_siteim_notread_count.style.display = "";
    ele_siteim_notread_count.innerHTML = siteim_notread_count;

    while (siteim_p2p_chat_ul.childNodes.length > siteim_group_chat_displayed_maxcount) {
        siteim_p2p_chat_ul.removeChild(siteim_p2p_chat_ul.childNodes[0]);
        siteim_group_chat_displayed_count--;
    }

},

P2P_CHAT_NTY_content: function (ws, event, commandName, bodyObj, msgCount, container) {


    if (bodyObj.ct == 1) {
        container.innerHTML = bodyObj.c;
    } else if (bodyObj.ct == 2) {  //超链接卡片
        var c = JSON.parse(bodyObj.c);

        var div_wrap = document.createElement("div");
        container.appendChild(div_wrap);
        $(div_wrap).addClass("card_link_wrap");

        var a = document.createElement("a");
        div_wrap.appendChild(a);
        $(a).attr("href", c.link);
        $(a).attr("target", "_blank");



        var div_a = document.createElement("div");
        a.appendChild(div_a);

        var div_title = document.createElement("div");
        div_a.appendChild(div_title);
        $(div_title).addClass("card_link_title ka");
        div_title.innerHTML = c.title;

        var div_main = document.createElement("div");
        div_a.appendChild(div_main);
        $(div_main).addClass("card_link_main");
        div_main.innerHTML = "<pre>" + c.sum + "</pre>";

        var div_footer = document.createElement("div");
        div_a.appendChild(div_footer);
        $(div_footer).addClass("card_link_footer");
        div_footer.innerHTML = '<span class="layui-badge-rim badge">' + siteim_const.linkcard.ct[c.contentType] + '</span>'
    } else if (bodyObj.ct == 5) {  //视频卡片
        var videos = JSON.parse(bodyObj.c);
        log("videos", videos);
        var div_wrap = document.createElement("div");
        container.appendChild(div_wrap);
        var html = "";

        if (!videos.length) {
            videos = [videos];
        }
        for (var index = 0; index < videos.length; index++) {
            
            var video = videos[index];

            var coverurl = video.coverurl || video.cover;
            var title = video.title || video.filename;
            // if (video.seconds && video.seconds > 0) {
            //     title += "(时长:" + video.formatedseconds + ")";
            // }
            // if (video.size && video.size > 0) {
            //     title += "(大小:" + (video.size / 1024).toFixed(2) + "KB)";
            // }
            
            var videourl = video.url || video.path;

            // html += '<video src="' + res_url(video.path) + '" width="240px" height="180px" controls="controls">';
            // html += '</video>';

            var id = bodyObj.mid + '_' + index;
            html += '<div class="card_video_wrap" id="'+ id + '" title="' + title + '" '
            if (coverurl) {
                // var scaleH =  210 / video.coverheight;   //以高为准
                var scaleW =  200 / video.coverwidth;      //以宽为准
                var scale = scaleW;//Math.min(scaleW, scaleH);

                var newWidth = video.coverwidth * scale;
                var newHeight = video.coverheight * scale;

                html += 'style="background-image:url(' + res_url(coverurl) + ');width:'+newWidth+'px;height:'+newHeight+'px;"';
                //background-image: url("/im/site/img/video-cover.jpeg");
            }
            html += '>';

            html += '<div class="filename ka">' + video.filename + '</div>'; //文件名
            html += '<div class="formatedseconds ka">' + video.formatedseconds + '</div>'; //时长
            
            html += '<i class="layui-icon">&#xe652;</i>';
            html += '</div>';

            var loadstr = '<div style="height:100%;width:100%;"><video width="100%" height="99%"  controls="controls" loop="loop"  autobuffer="autobuffer"  autoplay="autoplay" ><source src="' + res_url(videourl) + '" ></source></video></div>';

            //点击时的处理者
            var scale1 = 1000 / video.width;
            var scale2 = 666 / video.height;
            var s = Math.min(scale1, scale2);

            var w = video.width * s + 20;
            var h = video.height * s + 20;
            var hh = new siteim.cardvideo_click(
                {
                    type:1,
                    title: title + "（来自：" + bodyObj.f.n + "）",
                    area: [w + 'px', h + 'px'],
                    shade: 0,
                    closeBtn: 1,
                    "id": "_" + id,   //注意这个id不能和html里的元素id一样
                    maxmin:true,
                    content: loadstr,
                }
            ).h;
            //给前面的a链接添加click事件，动态添加的dom，所以用下面这种方法添加事件
            $(siteim_p2p_chat_ul).on("click", "#" + id, hh);

        }

        div_wrap.innerHTML = html;
    } else if (bodyObj.ct == 6) {  //图片卡片
        var imgs = JSON.parse(bodyObj.c);

        var div_wrap = document.createElement("div");
        container.appendChild(div_wrap);
        $(div_wrap).addClass("card_img_wrap");

        var len = imgs.length;
        if (len == 1) {
            $(div_wrap).css("grid-template-columns", "repeat(1, 1fr)");
        } else if (len <= 6) {
            $(div_wrap).css("grid-template-columns", "repeat(2, 1fr)");
        } else {
            $(div_wrap).css("grid-template-columns", "repeat(3, 1fr)");
        }


        //先循环一遍构造layer.photos需要的json数据
        var lay_photos_data = [];
        for (var i = 0; i < len; i++) {
            var cardImg = imgs[i];
            var img_id = "lay_photos_img_" + bodyObj.mid + "_" + i;
            var smurl = cardImg.coverurl || cardImg.small;
            var url = cardImg.url || cardImg.path;
            lay_photos_data[i] =
            {
                "alt": "I Love T-io",
                "pid": img_id,
                "src": res_url(url), //原图地址
                "thumb": res_url(smurl) //缩略图地址
            };
        }

        for (var j = 0; j < len; j++) {
            var cardImg = imgs[j];
            var smurl = cardImg.coverurl || cardImg.small;

            var div = document.createElement("div");
            div_wrap.appendChild(div);
            $(div).addClass("box");

            var img = document.createElement("img");
            div.appendChild(img);
            var img_id = "lay_photos_img_" + bodyObj.mid + "_" + j;
            // $(img).addClass("box");
            $(img).attr("src", res_url(smurl));
            $(img).attr("id", img_id);

            //点击图片时的处理者
            var hh = new siteim.cardimg_click(
                {
                    photos: {
                        "title": "", //相册标题
                        "id": bodyObj.mid, //相册id
                        "start": j, //初始显示的图片序号，默认0
                        "data": lay_photos_data
                    }, //格式见API文档手册页
                    anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机
                }
            ).h;
            //给前面的a链接添加click事件，动态添加的dom，所以用下面这种方法添加事件
            $(siteim_p2p_chat_ul).on("click", "#" + img_id, hh);
        }
    } else {
        container.innerHTML = bodyObj.c;
    }



    //图片加载完成后，自动滚屏
    $(container).find("img").each(function () {
        var self = $(this);
        var src = self.attr("src");
        var img = new Image();
        img.src = self.attr("src");
        img.onload = function () {
            siteim.p2p_scroll_chat();
        };

        img.onerror = function () {
            self.remove();
            // $(this).attr("src");
        };
    });
}